package com.taomu.app.zongchouhuli.weight.popwindow.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;

import java.util.ArrayList;
import java.util.List;

public class SingleSelectAdp extends RecyclerView.Adapter<SingleSelectAdp.SingleSelectHolder>{

    private List<String> mData;

    private LayoutInflater inflater;

    private Context mContext;

    public SingleSelectAdp(Context mContext){
        this.mContext = mContext;
        this.mData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
    }

    public void setData(List<String> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void addData(List<String> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public SingleSelectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_single_select,parent,false);
        return new SingleSelectHolder(view);
    }

    @Override
    public void onBindViewHolder(SingleSelectHolder holder, int position) {
        final String entity = mData.get(position);
        holder.update(entity);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onItemClick(entity);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class SingleSelectHolder extends RecyclerView.ViewHolder{

        private TextView mSingleTv;

        public SingleSelectHolder(View itemView) {
            super(itemView);
            mSingleTv = itemView.findViewById(R.id.item_single_tv);
        }

        public void update(String entity){
            mSingleTv.setText(entity);
        }
    }

    private SingleAdpListener listener;

    public void setSingleAdpListener(SingleAdpListener listener){
        this.listener = listener;
    }

    public interface SingleAdpListener{
        void onItemClick(String entity);
    }
}
